﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using SharpShare.Storage;
using System.ComponentModel;
using SharpShare.Storage.Filter;

namespace SharpShare.Configuration {
    public class ShareElement : ConfigurationElement, IKeyedElement, IStorageFilter {
        [Category("(General)")]
        [Description("The name of this share used to identify and reference this instance in the configuration.")]
        [ConfigurationProperty("name", IsKey = true, IsRequired = true)]
        public string Name {
            get {
                return (string)this["name"];
            }
            set {
                this["name"] = value;
            }
        }

        [Category("Content")]
        [DisplayName("Storage Provider")]
        [Description("The storage provider that will provide the content of this share.")]
        [TypeConverter(typeof(StorageProviderNameConverter))]
        [ConfigurationProperty("storageProvider")]
        public string StorageProvider {
            get {
                return (string)this["storageProvider"];
            }
            set {
                this["storageProvider"] = value;
            }
        }

        [Category("Content")]
        [Description("When specified, overrides the actual total space available reported by the storage provider.")]
        [DisplayName("Total Space Available")]
        [DefaultValue(null)]
        [TypeConverter(typeof(ShareSizeConverter))]
        [ConfigurationProperty("size", DefaultValue = null)]
        public ulong? Size {
            get {
                return (ulong?)this["size"];
            }
            set {
                this["size"] = value;
            }
        }

        public virtual void ConfigureService(IShareService service, IStorageProvider storageProvider, IConfigurationContext context) {

        }

        public virtual IStorageProvider GetShareStorageProvider(IStorageProvider storageProvider, IConfigurationContext context) {
            if (!this.Size.HasValue && this.Name.Equals(storageProvider.Name)) {
                return storageProvider;
            }

            FilteredStorageProvider filteredProvider = new FilteredStorageProvider(storageProvider, this);

            return filteredProvider;
        }

        #region IStorageFilter Members

        string IStorageFilter.ModifyName(FilterScope<IStorageItem> item, string name) {
            if (item.FilteredItem.Parent == null) {
                return this.Name;
            }

            return name;
        }

        IStorageLookup IStorageFilter.ModifyContents(FilterScope<IStorageContainer> container, IStorageLookup contents) {
            return contents;
        }

        #endregion

        #region IKeyedElement Members

        string IKeyedElement.Key {
            get { return this.Name; }
        }

        #endregion

        private class ShareSizeConverter : BytesTypeConverter {
            public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) {
                return new StandardValuesCollection(new object[] { null });
            }
            public override bool GetStandardValuesSupported(ITypeDescriptorContext context) {
                return true;
            }
            public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) {
                return false;
            }
            public override bool IsValid(ITypeDescriptorContext context, object value) {
                return true;
            }
            public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) {
                if (destinationType == typeof(string) && value == null)
                    return "Same as storage provider";

                return base.ConvertTo(context, culture, value, destinationType);
            }
        }
    }


}
